package com.springmvc.demo.controller;

import com.springmvc.demo.model.UserInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/lpy")
public class UserController {

    // RequestMapping 其实就是路由映射，也就是建立客户端与服务器的连接
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() {
        return "hello";
    }

    @RequestMapping("/number")
    public String r1(Integer age) {
        return "age为：" + age;
    }

    @RequestMapping("/r2")
    public String r2(UserInfo user) {
        return user.toString();
    }

    // 变名，如果不传这个参数的时候加上 "required"
    @RequestMapping("/r3")
    public String r3(@RequestParam(value = "name", required = false) String username, Integer age) {
        return "username为：" + username + " age为：" + age;
    }

    // 传递数组
    @RequestMapping("/r4")
    public String r4(String[] arr) {
        return Arrays.toString(arr);
    }

    // 传递集合的时候 需要加注解
    @RequestMapping("/r5")
    public String r5(@RequestParam(value = "list", required = false) List<String> list) {
        if (list != null) {
           return list.toString();
        }
        return "list为空";
    }

    // 传递 json
    @RequestMapping("/r6")
    public String r6(@RequestBody UserInfo userInfo) {
        return userInfo.toString();
    }

    // 获取url上的参数
    @RequestMapping("/r7/{articleId}")
    public String r7(@PathVariable Integer articleId) {
        return "articleId" + articleId;
    }

    // 上传文件
    @RequestMapping("/r8")
    public String r8(@RequestPart MultipartFile file) {
        return "获取名称" + file.getOriginalFilename();
    }


}
